package org.tobuy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.tobuy.vo.*;
import org.tobuy.factory.*;

public class ShowUserInfo extends HttpServlet {


	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String path = "index.jsp?action=showUserInfo";
		String user_id = req.getParameter("user_id");
		User user = new User();
		try {
			user = UserDAOFactory.getIUserDAOInstance().findUser(user_id);
			System.out.println(user.getUser_id());
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<Score> buyScores = new ArrayList<Score>();
		List<Score> sellScores = new ArrayList<Score>();
		
		try {
			buyScores = ScoreDAOFactory.getIScoreDAOInstance().findBuyerScoreSet(user);
			System.out.println("****************************");
			sellScores = ScoreDAOFactory.getIScoreDAOInstance().findSellerScoreSet(user);
			System.out.println("****************************");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//		Iterator<Score> buyiter = buyScores.iterator();
//		int sum_buyscore=0;
//		while(buyiter.hasNext()){
//			Score score = buyiter.next();
//			sum_buyscore = score.getScore_value()+sum_buyscore;
//		}
//		int buyScore = sum_buyscore/buyScores.size();
//		
//		Iterator<Score> selliter = sellScores.iterator();
//		int sum_sellscore=0;
//		while(selliter.hasNext()){
//			Score score = selliter.next();
//			sum_sellscore = score.getScore_value()+sum_sellscore;
//		}
//		int sellScore = sum_sellscore/sellScores.size();
		
		System.out.println("servlet finished");
		req.setAttribute("buyscores", buyScores);
		req.setAttribute("sellscores", sellScores);
		System.out.println("servlet finished");
		req.setAttribute("user", user);
		System.out.println("servlet finished");
		req.getRequestDispatcher(path).forward(req, resp);

		
		

	
	}


	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);

	
	}

}
